package com.xy.manage.action;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tsurveyanswer;
import com.xy.manage.model.Tsurveypartner;
import com.xy.manage.model.Tsurveyquestion;
import com.xy.manage.service.TsurveyanswerService;
import com.xy.manage.service.TsurveypartnerService;
import com.xy.manage.service.TsurveyquestionService;



@Controller
public class TsurveypartnerAction{
	private static final Logger log = LoggerFactory.getLogger(TsurveypartnerAction.class);
	public TsurveypartnerAction() {
	}
 
 
 	/**
	 * 问卷参与者新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveypartnerAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tsurveypartner", new Tsurveypartner());
        setDict(model);
		return "tsurveypartnerEdit";
	}

	/**
	 * 问卷参与者新增保存
	 * 
	 * @param tsurveypartner
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tsurveypartnerSave.do")
	public String save(Tsurveypartner tsurveypartner, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tsurveypartner, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tsurveypartner = tsurveypartnerService.save(tsurveypartner);
		return "redirect:v_tsurveypartnerList.do";
	}

	/**
	 * 问卷参与者删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tsurveypartnerDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tsurveypartnerService.remove(id);
			}
		}
		return "redirect:v_tsurveypartnerList.do";
	}

	/**
	 * 问卷参与者编辑页面
	 * 
	 * @param partnerid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveypartnerEdit.do")
	public String edit(long partnerid, HttpServletRequest request, ModelMap model) {
		Tsurveypartner tsurveypartner = tsurveypartnerService.get(partnerid);
		model.addAttribute("tsurveypartner", tsurveypartner);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tsurveypartnerEdit";
	}

	/**
	 * 问卷参与者详情
	 * 
	 * @param partnerid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveypartnerDetail.do")
	public String detail(long partnerid, HttpServletRequest request, ModelMap model) {
		Tsurveypartner tsurveypartner = tsurveypartnerService.get(partnerid);
//		Map<String,Object> map = new HashMap<String, Object>();
//		map.put("", null);
//		Tsurveyanswer
		model.addAttribute("tsurveypartner", tsurveypartner);
        setDict(model);
		return "tsurveypartnerView";
	}

	/**
	 * 问卷参与者查询（分页）
	 * 
	 * @param tsurveypartner
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveypartnerList.do")
	public String listPage(Tsurveypartner tsurveypartner, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tsurveypartnerService.getTsurveypartnerByPage(toMap(tsurveypartner),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
		//统计
		if(null!=tsurveypartner.getSurverytype()){
			Map<String, Object> queryMap = new HashMap<String, Object>();
			queryMap.put("surverytype", tsurveypartner.getSurverytype());
			List<Tsurveyquestion> list = tsurveyquestionService.getTsurveyquestion(queryMap);
			for(Tsurveyquestion t : list){
				if("1".equals(t.getQuestiontype())||"2".equals(t.getQuestiontype())||"3".equals(t.getQuestiontype()))
					tsurveyanswerService.getCount(t);
			}
			model.addAttribute("questionList", list);
		}
        setDict(model);
		return "tsurveypartnerList";
	}

	/**
	 * 问卷参与者查询
	 * 
	 * @param tsurveypartner
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsurveypartnerList1.do")
	public String list(Tsurveypartner tsurveypartner, HttpServletRequest request, ModelMap model) {
		List<Tsurveypartner> list = tsurveypartnerService.getTsurveypartner(toMap(tsurveypartner));
		model.addAttribute("list", list);
        setDict(model);
		return "tsurveypartnerList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tsurveypartner
	 * @return
	 */	private Map<String,Object> toMap(Tsurveypartner tsurveypartner){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tsurveypartner.getPartnerid()!=null&&!tsurveypartner.getPartnerid().equals("")){
			map.put("partnerid", tsurveypartner.getPartnerid());
		}
		if(tsurveypartner.getSurverytype()!=null&&!tsurveypartner.getSurverytype().equals("")){
			map.put("surverytype", tsurveypartner.getSurverytype());
		}
		if(tsurveypartner.getPartnername()!=null&&!tsurveypartner.getPartnername().equals("")){
			map.put("partnername", tsurveypartner.getPartnername());
		}
		if(tsurveypartner.getPhone()!=null&&!tsurveypartner.getPhone().equals("")){
			map.put("phone", tsurveypartner.getPhone());
		}
		if(tsurveypartner.getCreatetime()!=null&&!tsurveypartner.getCreatetime().equals("")){
			map.put("createtime", tsurveypartner.getCreatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("surverytypeMap", DictUtils.getDictByType(DictUtils.TSURVEYPARTNER_SURVERYTYPE));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tsurveypartner tsurveypartner, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TsurveypartnerService tsurveypartnerService;   
	@Resource
	private TsurveyanswerService tsurveyanswerService;     
	@Resource
	private TsurveyquestionService tsurveyquestionService;  
}